<?php
$strSQL = "SELECT * FROM loaisanpham";
$result = mysqli_query($conn, $strSQL);
$products = array();
while ($row = mysqli_fetch_array($result)) {
    $products[] = $row;
}
foreach ($products as $key => $value) {
    $strSQL = "SELECT count(*) FROM sanpham WHERE ghichu='new' AND id_loai={$value['id_loai']}";
    $mysqli_result = mysqli_query($conn, $strSQL);
    $result = mysqli_fetch_array($mysqli_result);
    $products[$key]['numberRecords'] = $result[0];

    $strSQL1 = "SELECT * FROM sanpham WHERE id_loai={$value['id_loai']} AND ghichu='new' ORDER BY rand()";
    $mysqli_result1 = mysqli_query($conn, $strSQL1);
    while ($result1 = mysqli_fetch_array($mysqli_result1)) {
        $products[$key]['arrayData'][] = $result1;
    }
}
?>
<div>
    <table width="760" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td height="40" style="border-bottom:1px solid #333; background:url(images/toplist-content.gif) repeat-x; padding-bottom:5px; font-weight:bold"><font color="#FF0000">SẢN PHẨM MỚI </font><img src="images/new.gif" /></td>
        </tr>
    </table>
    <?php
    foreach ($products as $key => $value) {
        if ($value['numberRecords'] == 0) {
            continue; 
        } ?>	
        <table width="760" border="0" cellspacing="0" cellpadding="0">
            <tr>	
                <td style="padding-left:5px; width:550px; height:30px" background="images/blue_menu_1.png"><a href="?b=loaisp&idl=<?php echo $value['id_loai']; ?>" style="color: #FFF; font-size:14px; font-weight:bold"><img src="images/new-icon.png" /><?php echo $value['tenloaisp']; ?></a>
            </tr> 
            <tr>
                <td align="center" style="padding-left:5px;">
                    <?php foreach ($value['arrayData'] as $key1 => $value1) { ?>
                            <div class=divshow>
                                <table width=175 height=220 border=0 cellspacing=0 cellpadding=0 background="images/box.gif" style="border:1px dotted #999">
                                    <tr>
                                        <td height=170><a href=?b=chitiet&id=<?php echo $value1['id'] ?>><img src='sanpham/small/<?php echo $value1['hinh'] ?>' width=170px height=170 border=0> </a></td>
                                    </tr>
                                    <tr>
                                        <td height=25 style="font-size:14px; color:#F00"><a href=?b=chitiet&id=<?php echo $value1['id'] ?> class=a-m><strong><?php echo $value1['tensp'] ?></strong></a></td>
                                    </tr>
                                    <tr>
                                        <td height=25>Giá: <?php echo number_format($value1['gia'], 0, '', '.') ?> VND</td>
                                    </tr>
                                </table>		
                            </div>
                    <?php } ?>
                </td>
            </tr>
            <tr>
                <td align="right" style="padding-right:10px; border-top:1px solid #999"><a href="?b=loaisp&idl=<?php echo $value['id_loai']; ?>">Xem toàn bộ sản phẩm...</a></td>
            </tr>
            <?php } ?>
    </table>
</div>			